import mi from "../MI";

const { ccclass } = cc._decorator;


@ccclass
export default class TipView extends cc.Component {
    private lblTip: cc.Label = null;

    private _timeAlpha: number = 1;
    private _flagAlpha: boolean = false;
    private _durationAlpha: number = 1;


    protected onLoad(): void {
        this.lblTip = cc.find('lblTip', this.node).getComponent(cc.Label);
    }

    protected update(dt: number): void {
        if (this._flagAlpha) {
            this._timeAlpha -= dt;
            if (this._timeAlpha <= 0) {
                this.node.destroy();
            }
            var opacityRatio = this._timeAlpha / this._durationAlpha;
            this.node.opacity = opacityRatio * 255;
        }
    }


    public startTip(content: string): void {
        this.lblTip.string = content;
        // 2.0 版本后 Label 节点设置 string 内容后并不会在这一帧马上更新，需要强制更新
        this.lblTip['_forceUpdateRenderData'](true);

        var up = cc.moveBy(1.5, cc.v2(0, mi.UIRoot.getContentSize().height / 2 - 100));
        var upCallback = cc.callFunc(this.upCallback, this);

        this.node.runAction(cc.sequence(up, upCallback));
    }


    private upCallback(): void {
        this._flagAlpha = true;
    }
}
